#include "../sqlconnpool.h"
#include <iostream>

// 测试用例
void TestSqlConnPool() {
    // 初始化连接池
    SqlConnPool::Instance()->Init("localhost", 3307, "root", "000605", "webserver", 5);

    // 检查连接池状态
    std::cout << "Free connection count before test: " << SqlConnPool::Instance()->GetFreeConnCount() << std::endl;

    // 获取连接并进行简单查询测试
    MYSQL* conn = nullptr;
    {
        SqlConnRAII raii_conn(&conn, SqlConnPool::Instance());
        if (conn) {
            if (mysql_query(conn, "SELECT 'Hello, World!'")) {
                LOG_ERROR("MySQL query error: %s", mysql_error(conn));
                std::cout << "MySQL query error:" << mysql_error(conn) << std::endl;
            } else {
                MYSQL_RES* result = mysql_store_result(conn);
                if (result) {
                    MYSQL_ROW row = mysql_fetch_row(result);
                    if (row)
                        std::cout << "Query result: " << row[0] << std::endl;
                    mysql_free_result(result);
                }
                std::cout << "Query result is null " << std::endl;
            }
        }
        std::cout << "conn is null " << std::endl;
    }  // 这里 SqlConnRAII 析构，会自动调用 FreeConn()

    // 检查连接池状态
    std::cout << "Free connection count after test: " << SqlConnPool::Instance()->GetFreeConnCount() << std::endl;

    // 关闭连接池
    SqlConnPool::Instance()->ClosePool();
}

// int main() {
//     Log::Instance()->init(0, "./log", ".log", 10);
//     LOG_INFO("========== Server init ==========");

//     TestSqlConnPool();
//     return 0;
// }

/*
g++ sqlconnpool_testcase.cpp ../*.cpp ../../Log/*.cpp ../../Buffer/*.cpp -lpthread -lmysqlclient -o test
*/